package com.sunnyweather.android.logic.dao

import android.content.Context
import androidx.core.content.edit
import com.google.gson.Gson
import com.sunnyweather.android.SunnyWeatherApplication
import com.sunnyweather.android.logic.model.Weather

object WeatherDao {

    private fun sharedPreference()=SunnyWeatherApplication.context.getSharedPreferences("sunny_weather", Context.MODE_PRIVATE)

    fun addWeather(key:String,weather: Weather){
        sharedPreference().edit {
            putString(key,Gson().toJson(weather))
        }
    }

    fun getWeather(key:String):Weather= Gson().fromJson(sharedPreference().getString(key,""),Weather::class.java)

    fun removeWeather(key:String){
        sharedPreference().edit {
            remove(key)
        }
    }

    fun isExist(key:String)= sharedPreference().contains(key)
}